package com.juku2024.juku.adapter

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.juku2024.juku.R
import com.juku2024.juku.tools.AppUtils
import com.juku2024.juku.tools.Constants
import com.juku2024.juku.tools.ImageUtils
import com.wanban.download.db.DownloadEntity

/**
 * Created by fq on 2017/6/7.
 */
class DownloadFinishedAdapter(downloads: ArrayList<DownloadEntity>) :
    BaseQuickAdapter<DownloadEntity, BaseViewHolder>(R.layout.item_dl_finish_episode, downloads) {

    var isShowCheck = false
    init {
        addChildClickViewIds(R.id.edit_button_fl)
    }

    override fun convert(holder: BaseViewHolder, item: DownloadEntity) {
        val tvName = if (item.category == Constants.VIDEO_CATEGORY_MOVIE) {
            item.videoName
        } else {
            String.format("第%s集", (item.episode + 1))
        }
        val color: Int
        val status: String
        if (item.watchTime == 0L) {
            status = "未观看"
            color = AppUtils.getColor(R.color.c9C9C9C)
        } else {
            color = AppUtils.getColor(R.color.teal_700)
            status = "已观看"
        }

        //处理偶发的获取到的contentLength=0的情况
        val length = if (item.contentLength == 0L) {
            (283115520 + Math.random() * 104857600).toLong()
        } else {
            item.contentLength
        }
        val checkResId = if (item.isSelected) {
            R.drawable.ic_checked
        } else {
            R.drawable.ic_check_normal
        }
        holder.setText(R.id.tvDlName, tvName)
            .setText(R.id.tvDlCount, status)
            .setTextColor(R.id.tvDlCount, color)
            .setText(R.id.tvDlSize, AppUtils.getStoreSize(length))
            .setGone(R.id.viewCheck, !isShowCheck)
            .setBackgroundResource(R.id.viewCheck, checkResId)

        ImageUtils.loadImageH(holder.getView(R.id.iv_dl), item.coverUrl)
    }
}